package com.example.baseapp.dto;

import com.example.baseapp.entity.OrderInfo;
import com.example.baseapp.entity.OrderDetail;
import com.example.baseapp.common.OrderStatusEnum;
import lombok.Data;
import java.util.List;
import java.util.ArrayList;
import java.math.BigDecimal;

@Data
public class OrderDetailDTO {
    // 订单基本信息
    private Long id;
    private String orderId; // 订单号
    private Long userId;
    private String userName;
    private String phone;
    private String address;
    private String department;
    private String remark;
    private Integer status;
    private String statusText;
    private BigDecimal totalPrice;
    private BigDecimal discountAmount;
    private String createTime;
    private String updateTime;
    
    // 订单商品列表
    private List<OrderItemDTO> orderItems;
    
    public OrderDetailDTO(OrderInfo orderInfo, List<OrderDetail> orderDetails) {
        this.id = orderInfo.getId();
        this.orderId = orderInfo.getOrderNo();
        this.userId = orderInfo.getUserId();
        this.userName = orderInfo.getUserName();
        this.phone = orderInfo.getUserPhone();
        this.address = orderInfo.getUserAddress();
        this.department = orderInfo.getUserDepartment();
        this.remark = orderInfo.getRemark();
        this.status = convertStatusToInt(orderInfo.getStatus());
        this.statusText = OrderStatusEnum.getStatusText(orderInfo.getStatus());
        this.totalPrice = orderInfo.getTotalAmount();
        this.discountAmount = BigDecimal.ZERO; // 暂时设为0，后续可扩展
        this.createTime = orderInfo.getCreateTime() != null ? orderInfo.getCreateTime().toString() : null;
        this.updateTime = orderInfo.getUpdateTime() != null ? orderInfo.getUpdateTime().toString() : null;
        
        // 转换订单明细 - 使用简单的循环避免Stream API问题
        this.orderItems = new ArrayList<>();
        if (orderDetails != null) {
            for (OrderDetail orderDetail : orderDetails) {
                this.orderItems.add(new OrderItemDTO(orderDetail));
            }
        }
    }
    
    private Integer convertStatusToInt(String status) {
        if (status == null) return 0;
        switch (status) {
            case "PENDING": return 0;
            case "PAID": return 1;
            case "PREPARING": return 2;
            case "DELIVERING": return 3;
            case "COMPLETED": return 4;
            case "CANCELLED": return 5;
            default: return 0;
        }
    }
    
    @Data
    public static class OrderItemDTO {
        private Long id;
        private Long dishId;
        private String dishName;
        private String dishDescription;
        private String dishImage;
        private BigDecimal price;
        private Integer quantity;
        private BigDecimal subtotal;
        
        public OrderItemDTO(OrderDetail orderDetail) {
            this.id = orderDetail.getId();
            this.dishId = orderDetail.getDishId();
            this.dishName = orderDetail.getDishName() != null ? orderDetail.getDishName() : "未知菜品";
            this.dishDescription = orderDetail.getDishDescription() != null ? orderDetail.getDishDescription() : "";
            this.dishImage = orderDetail.getDishImage() != null ? orderDetail.getDishImage() : "https://img.yzcdn.cn/vant/cat.jpeg";
            this.price = orderDetail.getPrice();
            this.quantity = orderDetail.getQuantity();
            this.subtotal = orderDetail.getSubtotal();
        }
    }
} 